package com.chance.cc.crawler.prod.command.job.domain.news.iqiyi.searchKw;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.key;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/4/11 12:19
 * @Description
 *      爱奇艺
 **/
public class IQYArticleSimpleCrawlerSchedulerJob extends NewsCommonScript {
    public static final String domain = "iqiyi";//-
    public static final String site = "searchKw";//-
    public static final String resultQueueName = StringUtils.joinWith("-", "crawler", domain, site, "simple", "queue");//-
    public static void main(String[] args) {
        publishIQYArticleSimpleCrawlerScheduleJobInfo();//-
    }

    public static CrawlerJob publishIQYArticleSimpleCrawlerScheduleJobInfo(){//-
        CrawlerJob crawlerJob = iQYArticleSimpleCrawlerSchedulejob();//-

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("爱奇艺临时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;

    }

    public static CrawlerJob iQYArticleSimpleCrawlerSchedulejob(){//-
        String url = "https://www.iqiyi.com/v_232u2njtow0.html";
        String siteBiz = "video";

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 2,null))
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
//                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        crawlerRequestRecord.setDownload(false);
        crawlerRequestRecord.setSkipPipeline(true);
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(key);
        commentFilter.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,siteBiz,"comment","queue")));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        crawlerRequestRecord.getHttpRequest().addExtra("searchKwSourceUrl","https://so.iqiyi.com/so/q_%s_ctg__t_0_page_1_p_1_qc_0_rd_1_site_iqiyi_m_4_bitrate__af_1");//一天
//        crawlerRequestRecord.getHttpRequest().addExtra("searchKwSourceUrl","https://so.iqiyi.com/so/q_%s_ctg__t_0_page_1_p_1_qc_0_rd_2_site_iqiyi_m_4_bitrate__af_1");//一周

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,siteBiz,CrawlerMetaConstant.ScheduleJobTrigger_Simple))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(resultQueueName))
//                .fileResultPipeline(null, "/data/chance_crawler_test/data/acfun/acfun_searchKw_video_realtime.log", false)
                .kafkaResultPipeline(null,kafkaTopic,null)
                .requestRecord(crawlerRequestRecord)
                .build();
    }
}
